<?php

global $_W, $_GPC;

$_GPC['menu'] = 'data';

if ($_GPC['act'] == 'mutdelete') {
    $ids = $_GPC['ids'];
    foreach ($ids as $id) {
        M('article')->delete($id);
    }
    message('删除成功', referer(), 'success');
}
if ($_GPC['act'] == 'edit') {
    $id = intval($_GPC['id']);
    if ($_W['ispost']) {
        $data = array();
        $data['uniacid'] = $_W['uniacid'];

        if (!empty($_GPC['category_id'])) {
            $data['category_id'] = intval($_GPC['category_id']);
        } else {
            message('请选择分类', $this->createWebUrl('article', array('act' => 'edit')), 'error');
        }

        if (!empty($_GPC['title'])) {
            $data['title'] = trim($_GPC['title']);
        } else {
            message('请填写标题', $this->createWebUrl('article', array('act' => 'edit')), 'error');
        }

        if (!empty($_GPC['thumb'])) {
            $data['thumb'] = trim($_GPC['thumb']);
        } else {
            message('请上传缩略图', $this->createWebUrl('article', array('act' => 'edit')), 'error');
        }

        if (!empty($_GPC['content'])) {
            $data['content'] = trim($_GPC['content']);
        } else {
            message('请填写内容', $this->createWebUrl('article', array('act' => 'edit')), 'error');
        }

        if (!empty($_GPC['writer'])) {
            $data['writer'] = trim($_GPC['writer']);
        } else {
            $data['writer'] = '未知';
        }

        if (!empty($_GPC['link'])) {
            $data['link'] = trim($_GPC['link']);
        }

        if (!empty($_GPC['sort'])) {
            $data['sort'] = intval($_GPC['sort']);
        }

        $data['status'] = intval($_GPC['status']);

        if (!empty($id)) {
            $data['id'] = $id;
            $data['update_time'] = time();
            $question = M('article')->update($data);
        } else {
            $data['create_time'] = time();
            $question = M('article')->update($data);
        }
        message('保存成功', $this->createWebUrl('article'), 'success');
    }
    $item = M('article')->getInfo($id);
    $category = M('category')->getall();
    include $this->template('article_edit');
    exit;
}


if ($_GPC['act'] == 'delete') {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        if ($_W['ispost']) {
            $data = array();
            $data['status'] = 1;
            $data['message'] = '参数错误';
            die(json_encode($data));
        } else {
            message('参数错误', referer(), 'error');
        }
    }
    M('article')->delete($id);
    if ($_W['ispost']) {
        $data = array();
        $data['status'] = 1;
        $data['message'] = '操作成功';
        die(json_encode($data));
    } else {
        message('删除成功', referer(), 'success');
    }
}


$page = !empty($_GPC['page']) ? intval($_GPC['page']) : 1;
$where = "";
if ($categoryId = $_GPC['category_id']) {
    $where .= " AND category_id = {$categoryId}";
}
if ($_GPC['status'] != '') {
    $where .= " AND status = {$_GPC['status']}";
}
if ($tipsContent = $_GPC['title']) {
    $where .= " AND title like '%$tipsContent%'";
}

$list = M('article')->getList($page, $where);
$categoryArr = M('category')->getall();


if (!empty($list['list'])) {
    foreach ($list['list'] as $key => $value) {
        $category = M('category')->getInfo($value['category_id']);
        $list['list'][$key]['category_name'] = $category['name'];
        $list['list'][$key]['status'] = $value['status'] == 1 ? '显示' : '隐藏';
    }
}

include $this->template('article');